﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace AirlineCommon
{
    public class EconomyPassenger : PassengerCommon
    {
        public override void InsertPassenger()
        {
            string InsertSql = "INSERT INTO [Passengers] ([PNRNo], [FlightNo], [TravelDate], [FName], [Lname], [Age], [Gender], [Class], [SeatPref], [MealPref], [SSR], [Status]) VALUES (@PNRNo, @FlightNo, @TravelDate, @FName, @Lname, @Age, @Gender, @Class, @SeatPref, @MealPref, @SSR, @Status)";
            SqlConnection conn = new SqlConnection(DAL.DatabaseProvider.AirlineConnectionString);
            SqlParameter pPNRNo = new SqlParameter("@PNRNo", base.PNRNo);
            SqlParameter pFlightNo = new SqlParameter("@FlightNo", base.FlightNo);
            SqlParameter pTravelDate = new SqlParameter("@TravelDate", base.TravelDate);
            SqlParameter pFName = new SqlParameter("@FName", base.FName);
            SqlParameter pLname = new SqlParameter("@Lname", base.LName);
            SqlParameter pAge = new SqlParameter("@Age", base.Age);
            SqlParameter pGender = new SqlParameter("@Gender", base.Gender);
            SqlParameter pClass = new SqlParameter("@Class", base.Class);
            SqlParameter pSeatPref = new SqlParameter("@SeatPref", string .Empty );
            SqlParameter pMealPref = new SqlParameter("@MealPref", base.MealPref);
            SqlParameter pSSR = new SqlParameter("@SSR",string .Empty );
            SqlParameter pStatus = new SqlParameter("@Status", Status);
            int HowManyRows = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, InsertSql,
                pPNRNo,
                pFlightNo,
                pTravelDate,
                pFName,
                pLname,
                pAge,
                pGender,
                pClass,
                pSeatPref,
                pMealPref,
                pSSR,
                pStatus);
            if (HowManyRows != 1)
                throw new Exception("Insert passenger fail.");
            // sqlComm.Dispose();
            conn.Close();
            return;
        }

        public override int InsertPassenger(SqlTransaction trans)
        {
            try
            {
                string InsertSql = "INSERT INTO [Passengers] ([PNRNo], [FlightNo], [TravelDate], [FName], [Lname], [Age], [Gender], [Class], [SeatPref], [MealPref], [SSR], [Status]) VALUES (@PNRNo, @FlightNo, @TravelDate, @FName, @Lname, @Age, @Gender, @Class, @SeatPref, @MealPref, @SSR, @Status)";
                //SqlConnection conn = new SqlConnection(DAL.DatabaseProvider.AirlineConnectionString);
                SqlParameter pPNRNo = new SqlParameter("@PNRNo", base.PNRNo);
                SqlParameter pFlightNo = new SqlParameter("@FlightNo", base.FlightNo);
                SqlParameter pTravelDate = new SqlParameter("@TravelDate", base.TravelDate);
                SqlParameter pFName = new SqlParameter("@FName", base.FName);
                SqlParameter pLname = new SqlParameter("@Lname", base.LName);
                SqlParameter pAge = new SqlParameter("@Age", base.Age);
                SqlParameter pGender = new SqlParameter("@Gender", base.Gender);
                SqlParameter pClass = new SqlParameter("@Class", base.Class);
                SqlParameter pSeatPref = new SqlParameter("@SeatPref", string.Empty);
                SqlParameter pMealPref = new SqlParameter("@MealPref", base.MealPref);
                SqlParameter pSSR = new SqlParameter("@SSR", string.Empty);
                SqlParameter pStatus = new SqlParameter("@Status", Status);
                int HowManyRows = SqlHelper.ExecuteNonQuery(trans, CommandType.Text, InsertSql,
                    pPNRNo,
                    pFlightNo,
                    pTravelDate,
                    pFName,
                    pLname,
                    pAge,
                    pGender,
                    pClass,
                    pSeatPref,
                    pMealPref,
                    pSSR,
                    pStatus);
                return HowManyRows;
            }
            catch
            {
                return -1;
            }
        }

        //public override int UpdatePassengerStatus(SqlTransaction trans, string PNRNo, string status)
        //{
        //    try
        //    {
        //        //SqlConnection conn = new SqlConnection(DAL.DatabaseProvider.AirlineConnectionString);
        //        string UpdateSql = "Update Passengers set Status=@Status Where PNRNo=@PNRNo";
        //        SqlParameter pStatus = new SqlParameter("@Status", status);
        //        SqlParameter pPNRno = new SqlParameter("@PNRNo", PNRNo);
        //        int HowManyRows = SqlHelper.ExecuteNonQuery(trans, CommandType.Text, UpdateSql, pStatus,
        //               pPNRno);
        //        return HowManyRows;
        //    }
        //    catch
        //    {
        //        return -1;
        //    }
        //}
    }
}
